package com.shoulder.boot.acl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shoulder.boot.acl.dto.M2rDTO;
import com.shoulder.boot.acl.dto.RoleAddDTO;
import com.shoulder.boot.acl.dto.RoleModDTO;
import com.shoulder.boot.acl.dto.RoleQueryDTO;
import com.shoulder.boot.acl.entity.Role;
import com.shoulder.boot.acl.service.RoleService;
import com.shoulder.boot.acl.vo.RoleVo;
import com.shoulder.core.api.ApiResult;
import com.shoulder.core.api.Kv;
import com.shoulder.core.fetch.BaseIds;
import com.shoulder.core.page.PageSchema;
import com.shoulder.log.annotation.LogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/api/v1/acl/role")
public class RoleController {

    private final RoleService roleService;

    /**
     * 获取数据权限范围
     */
    @GetMapping("/scope/enums")
    @LogAnnotation(type = "获取数据权限范围", record = false)
    public ApiResult<List<Kv>> scopeEnums() {
        List<Kv> roles = this.roleService.scopeEnums();
        return ApiResult.ok(roles);
    }

    /**
     * 获取租户角色
     */
    @GetMapping("/tenant/only")
    @LogAnnotation(type = "获取租户角色", record = false)
    public ApiResult<List<Role>> getTenantRoles() {
        List<Role> roles = this.roleService.tenantRoles();
        return ApiResult.ok(roles);
    }

    /**
     * 获取角色权限id列表
     *
     * @param id id
     * @return 获取角色权限
     */
    @GetMapping("/menus/{id}")
    @LogAnnotation(type = "获取角色权限id列表", record = false)
    public ApiResult<List<String>> roleMenus(@PathVariable String id) {
        List<String> menus = this.roleService.roleMenus(id);
        return ApiResult.ok(menus);
    }

    /**
     * 角色授权
     *
     * @param m2rDTO 角色授权
     * @return ApiResult<Object>
     */
    @PostMapping("/m2r")
    @LogAnnotation(type = "角色授权")
    public ApiResult<Object> m2r(@RequestBody @Validated M2rDTO m2rDTO) {
        this.roleService.m2r(m2rDTO);
        return ApiResult.ok();
    }

    /**
     * 转换角色性质
     *
     * @param id id
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @PutMapping("/switch/{id}")
    @LogAnnotation(type = "转换角色性质")
    public ApiResult<Object> switchRole(@PathVariable String id) {
        this.roleService.switchRole(id);
        return ApiResult.ok();
    }


    /**
     * 添加角色信息
     *
     * @param addDTO 角色信息添加请求体
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @PostMapping
    @LogAnnotation(type = "添加角色信息")
    public ApiResult<Object> add(@RequestBody @Validated RoleAddDTO addDTO) {
        this.roleService.add(addDTO);
        return ApiResult.ok();
    }

    /**
     * 修改角色信息
     *
     * @param modDTO 角色信息编辑请求体
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @PutMapping
    @LogAnnotation(type = "修改角色信息")
    public ApiResult<Object> mod(@RequestBody @Validated RoleModDTO modDTO) {
        this.roleService.mod(modDTO);
        return ApiResult.ok();
    }

    /**
     * 查询角色信息分页数据
     *
     * @param queryDTO 角色信息查询请求体
     * @return ApiResult<PageSchema < Role>>
     * @author 596767880@qq.com
     */
    @LogAnnotation(type = "角色信息分页查询", record = false)
    @PostMapping("/page")
    public ApiResult<PageSchema<RoleVo>> pageQuery(@RequestBody RoleQueryDTO queryDTO) {
        Page<RoleVo> page = this.roleService.pageQuery(queryDTO);
        return ApiResult.ok(new PageSchema<>(page.getTotal(), page.getRecords()));
    }

    /**
     * 查询角色信息列表数据
     *
     * @param queryDTO 角色信息查询请求体
     * @return ApiResult<List < Role>>
     * @author 596767880@qq.com
     */
    @LogAnnotation(type = "角色信息列表查询", record = false)
    @PostMapping("/list")
    public ApiResult<List<RoleVo>> list(@RequestBody RoleQueryDTO queryDTO) {
        List<RoleVo> list = this.roleService.listQuery(queryDTO);
        return ApiResult.ok(list);
    }

    /**
     * 批量删除角色信息
     *
     * @param baseIds id集合
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @PostMapping("/rm")
    @LogAnnotation(type = "批量删除角色信息")
    public ApiResult<Object> delete(@RequestBody @Validated BaseIds<String> baseIds) {
        this.roleService.batchDel(baseIds);
        return ApiResult.ok();
    }


    /**
     * 根据id删除角色信息
     *
     * @param id 角色信息id
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @DeleteMapping("/{id}")
    @LogAnnotation(type = "删除角色信息")
    public ApiResult<Object> del(@PathVariable String id) {
        this.roleService.del(id);
        return ApiResult.ok();
    }


    /**
     * 批量查询角色信息
     *
     * @param baseIds id集合
     * @return ApiResult<List < Role>>
     * @author 596767880@qq.com
     */
    @PostMapping("/look")
    @LogAnnotation(type = "批量查询角色信息", record = false)
    public ApiResult<List<RoleVo>> looks(@RequestBody BaseIds<String> baseIds) {
        return ApiResult.ok(this.roleService.looks(baseIds));
    }

    /**
     * 根据id获取角色信息
     *
     * @param id 角色信息id
     * @return ApiResult<Role>
     * @author 596767880@qq.com
     */
    @GetMapping("/{id}")
    @LogAnnotation(type = "根据id查询角色信息", record = false)
    public ApiResult<RoleVo> look(@PathVariable String id) {
        return ApiResult.ok(this.roleService.look(id));
    }
}